package com.xyhuo.leetcode.january;

import java.util.*;

/**
 * 回旋镖的数量
 * https://leetcode.cn/problems/number-of-boomerangs/
 *
 * @time 2024/1/8
 */
public class NumberOfBoomerangs {

    public int numberOfBoomerangs(int[][] points) {
        int ans = 0;
        for (int i = 0; i < points.length; i++) {
            Map<Double, Integer> map = new HashMap<>();
            for (int j = 0; j < points.length; j++) {
                if (j == i) {
                    continue;
                }
                double x_2 = Math.pow(points[i][0] - points[j][0], 2);
                double y_2 = Math.pow(points[i][1] - points[j][1], 2);
                double key = x_2 + y_2;
                int count = 0;
                if (map.containsKey(key)) {
                    count = map.get(key);
                }
                map.put(key, count + 1);
            }
            for (Map.Entry<Double, Integer> entry : map.entrySet()) {
                if (entry.getValue() >= 2) {
                    ans += entry.getValue() * (entry.getValue() - 1);
                }
            }
        }
        return ans;
    }

}
